package homework.printyourself;

import homework.printyourself.danmu.view.DanMuPanel;
import homework.printyourself.info.BaseObject;
import homework.printyourself.info.Student;
import homework.printyourself.info.items.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws Exception {
	    BaseObject person = new Student();

        person = new NameItem(person);
	    person = new GenderItem(person);
	    person = new ClassItem(person);
	    person = new StudentIDItem(person);
	    person = new PhoneNumberItem(person);

	    System.out.print(person);

        JFrame frame = new JFrame("HelloWorld");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final DanMuPanel panel = new DanMuPanel(400, 400);
		frame.setContentPane(panel);

		final ArrayList<String> chain = person.getDescriptionChain();

        panel.startMovingTimer();


        final Timer t = new Timer(2000, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				for (String row: chain) {
					panel.addDanMu(row);
				}
			}
        });
		t.start();

		// 点击暂停
		frame.addMouseListener(new MouseListener() {

		    private boolean flag = true;
            @Override
            public void mouseClicked(MouseEvent mouseEvent) {
                if(flag) {
                    t.stop();
                    panel.pause();
                    flag = false;
                } else {
                    flag = !flag;
                    t.start();
                    panel.startMovingTimer();
                }
            }

            @Override
            public void mousePressed(MouseEvent mouseEvent) {}

            @Override
            public void mouseReleased(MouseEvent mouseEvent) {}

            @Override
            public void mouseEntered(MouseEvent mouseEvent) {}

            @Override
            public void mouseExited(MouseEvent mouseEvent) {}
        });

        frame.pack();
		frame.setVisible(true);
	}
}
